﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using DiscoveryLogic.UI.Layouts.Controls;
using System.Collections.Generic;
using DiscoveryLogic.UI.Layouts.ContentScreens;
using DiscoveryLogic.UI.Layouts.Models;
using DiscoveryLogic.DataServices.DataSources;

namespace DiscoveryLogic.UI.Layouts.DataAcccesses
{
    public class FundsManager
    {
                #region Private Variables

            List<EnrollmentFund> listOfLocalFunds = null;
            List<EnrollmentFund> listOfWSFunds = null;

            private readonly bool enableWebService = false;

        #endregion

        #region Constructors

            public FundsManager()
            {               
            }

        #endregion


        #region Public Methods

          
            // Calls webservice and returns list of Account objects if WS is enabled.
            public List<EnrollmentFund> GetFunds()
            {
                //JAIMER  hack to get around async WS problem .. 
                // Chris: Commented out WS
                //if (enableWebService || this.WSAccounts == null )
                if (enableWebService)
                {
                    return this.WSFunds;
                }
                else
                {
                    return this.LocalFunds;
                }
            }

            private List<EnrollmentFund> LocalFunds
            {
                get
                {
                    if (listOfLocalFunds == null)
                    {
                        List<Fund> listOfFundObjs = new List<Fund>(LocalDataFiles.LoadData<Fund[]>("DiscoveryLogic.UI.Layouts.LocalDataFiles.Funds.xml"));

                        listOfLocalFunds = new List<EnrollmentFund>();

                        for (int i = 0; i < listOfFundObjs.Count; i++)
                        {                            
                            listOfLocalFunds.Add( new EnrollmentFund(listOfFundObjs[i].id, listOfFundObjs[i].fundType, listOfFundObjs[i].fundName, listOfFundObjs[i].fundValue, listOfFundObjs[i].fundContribution));                            
                        }
                    }
                    else
                    {
                        // Make a copy of the existing list because each scenario and 529 child
                        // needs its own set of funds that no one else references

                        List<EnrollmentFund> newListOfLocalFunds = new List<EnrollmentFund>();

                        for (int i = 0; i < listOfLocalFunds.Count; i++)
                        {
                            newListOfLocalFunds.Add(new EnrollmentFund(listOfLocalFunds[i].Id, listOfLocalFunds[i].FundType, listOfLocalFunds[i].FundName, listOfLocalFunds[i].FundValue, listOfLocalFunds[i].ContributionPercent));
                        }

                        return newListOfLocalFunds;
                    }

                    return listOfLocalFunds;
                }
            }

            private List<EnrollmentFund> WSFunds
            {
                get
                {
                    if (listOfWSFunds == null)
                    {
                        // Retrieve list of fund obj from WS
                    }
                    else
                    {
                        // Make a copy of the existing list because each scenario and child in 529
                        // needs its own set of funds that no one else references
                        List<EnrollmentFund> newlistOfWSFunds = new List<EnrollmentFund>();

                        for (int i = 0; i < listOfWSFunds.Count; i++)
                        {
                            newlistOfWSFunds.Add(new EnrollmentFund(listOfWSFunds[i].Id, listOfWSFunds[i].FundType, listOfWSFunds[i].FundName, listOfWSFunds[i].FundValue, listOfWSFunds[i].ContributionPercent));
                        }

                        return listOfWSFunds;
                    }

                    return listOfWSFunds;
                }
            }

          

        #endregion
    }
}
